<template>
  <el-form :inline="true" :model="ruleForm" ref="ruleForm" class="demo-ruleForm">
    <el-form-item label="关键字">
      <el-input v-model.trim="ruleForm.keyword" placeholder="根据文章标题搜索"></el-input>
    </el-form-item>

    <el-form-item label="状态">
      <el-select v-model="ruleForm.state">

        <el-option v-for="item in status" :key="item.value" :label="item.label" :value="item.value" />
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button @click="resetForm">清除</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')">搜索</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { status } from '@/api/hmmm/constants'
export default {
  name: 'Search',
  data() {
    return {
      ruleForm: {
        keyword: '',
        state: ''
      }
    }
  },
  computed: {
    status() {
      return status
    }
  },
  methods: {
    resetForm() {
      if (this.ruleForm.keyword || this.ruleForm.state !== '') {
        this.ruleForm = {
          keyword: '',
          state: ''
        }
      } else {
        this.$emit('clearForm')
      }
    },
    submitForm() {
      if (this.ruleForm.state !== '' || this.ruleForm.keyword) {
        const obj = {}
        for (const k in this.ruleForm) {
          this.ruleForm[k] && (obj[k] = this.ruleForm[k])
        }
        this.ruleForm.state !== '' && (obj.state = this.ruleForm.state)
        this.page = 1
        this.$emit('search', this.ruleForm)

        // // 如果没有状态 则不携带状态参数
        // if (this.ruleForm.state === '') return this.$emit('search', { keyword: this.ruleForm.keyword })
        // if (!this.ruleForm.keyword) return this.$emit('search', { state: this.ruleForm.state })
        // this.$emit('search', this.ruleForm)
      }
    }
  }
}
</script>

<style>
</style>
